package com.bcgtgjyb.huanwen.customview.mylibrary.star;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by bigwen on 2016/4/14.
 */
public class StarView extends Component implements Component.EstimateSizeListener,
        Component.DrawTask, Component.BindStateChangedListener {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private int width = 100;
    private int height = 100;
    private Paint paint;
    private Path pathBig;
    private int alpha = 255;
    private boolean isDirection = true;

    // 颜色变化率
    private int vv = 3;
    private boolean isInit = false;
    private Thread thread;
    private boolean isPlay = false;
    private float degree = 0;

    /**
     * 构造
     *
     * @param context 上下文
     */
    public StarView(Context context) {
        super(context);
        init();
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public StarView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        isPlay = true;
        addDrawTask(this);
        setEstimateSizeListener(this);
        setBindStateChangedListener(this);
        initThread();
    }

    private void initThread() {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isPlay) {
                    degree += 4;
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.fillInStackTrace();
                    }
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            invalidate();
                        }
                    });
                }
            }
        });
    }

    private void initPath() {
        int unit = width / 10;
        pathBig = new Path();
        pathBig.moveTo(unit * 5, 0);
        pathBig.lineTo(6 * unit, 4 * unit);
        pathBig.lineTo(10 * unit, 5 * unit);
        pathBig.lineTo(6 * unit, 6 * unit);
        pathBig.lineTo(5 * unit, 10 * unit);
        pathBig.lineTo(4 * unit, 6 * unit);
        pathBig.lineTo(0, 5 * unit);
        pathBig.lineTo(4 * unit, 4 * unit);
        pathBig.close();
    }

    /**
     * 设置
     *
     * @param dd float类型
     */
    public void setDegree(final float dd) {
        degree = dd;
        invalidate();
    }

    /**
     * 开始
     */
    public void start() {
        isPlay = true;
        thread.start();
    }

    /**
     * 结束
     */
    public void stop() {
        isPlay = false;
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (!isInit) {
            // 支持padding，不然padding属性无效
            int paddingLeft = getPaddingLeft();
            int paddingTop = getPaddingTop();
            int paddingRight = getPaddingRight();
            int paddingBottom = getPaddingBottom();
            width = getWidth() - paddingLeft - paddingRight;
            height = getHeight() - paddingTop - paddingBottom;
            initPath();
            isInit = true;
        }
        if (isDirection) {
            alpha -= vv;
        } else {
            alpha += vv;
        }
        if (alpha <= 0) {
            alpha = 0;
            isDirection = false;
        } else if (alpha >= 255) {
            alpha = 255;
            isDirection = true;
        } else {}
        paint.setColor(new Color(Color.argb(255,255,255, alpha)));
        canvas.rotate(degree, (float)((double)width / 2), (float)((double) height / 2));
        canvas.drawPath(pathBig, paint);
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        start();
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        stop();
    }
}
